package com.tm.szb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.szb.commons.R;
import com.tm.szb.dto.DishDTO;
import com.tm.szb.entity.Category;
import com.tm.szb.entity.Dish;
import com.tm.szb.service.CategoryService;
import com.tm.szb.service.DishService;
import com.tm.szb.vo.DishVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R dish(@RequestBody DishDTO dto){
        dishService.saveDish(dto);
        return R.success();
    }

    @GetMapping("page")
    public R page(Integer page,Integer pageSize,String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        dishService.page(pageInfo,new LambdaQueryWrapper<Dish>().eq(StringUtils.isNotBlank(name),Dish::getName,name)
                .orderByDesc(Dish::getUpdateTime));
        List<Dish> records = pageInfo.getRecords();
        List<DishVO> resList = new ArrayList<>();
        for (Dish record : records) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(record,dishVO);

            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                dishVO.setCategoryName(categoryName);
            }
            resList.add(dishVO);
        }
        Page<DishVO> resPageInfo = new Page<>(page,pageSize,pageInfo.getTotal());
        resPageInfo.setRecords(resList);
        return R.success(resPageInfo);
    }


    @DeleteMapping
    public R deleteDish(String ids){
        String[] split = ids.split(",");
        List<Long> collect = Arrays.stream(split).map(idStr -> {
            Long id = Long.parseLong(idStr);
            return id;
        }).collect(Collectors.toList());
        dishService.delDish(collect);
        return R.success();
    }


    @PostMapping("status/{type}")
    public R status(@PathVariable Integer type,String ids){
        String[] split = ids.split(",");
        List<Dish> dishList = Arrays.stream(split).map(idStr -> {
            Dish dish = new Dish();
            dish.setId(Long.parseLong(idStr));
            dish.setStatus(type);
            return dish;
        }).collect(Collectors.toList());
        dishService.updateBatchById(dishList);
        return R.success();
    }


    @GetMapping("/{id}")
    public R dish(@PathVariable Long id){
        DishVO dishVO = dishService.findById(id);
        return R.success(dishVO);
    }

    @PutMapping
    public R updateDish(@RequestBody DishDTO dishDTO){
        dishService.updateDish(dishDTO);
        return R.success();
    }



    @GetMapping("list")
    public R dishList(@RequestBody Long cateGoryId){
        List<DishVO> dishVOList = dishService.dishList(cateGoryId);
        return R.success(dishVOList);
    }
}
